﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.EstRec;
using System.Data.SqlClient;
using System.Data;

namespace BAL.EstRec
{
    public class FdvVersion
    {
        private long _unidFdvBgtVersion;
        private string _name;
        private DateTime _datePublished;
        private string _positionCode;
        private DateTime _dateLoaded;
        private long _refUnidHqbgtVersion;
        private DateTime _hqDateClosed;

        #region properties
        public long UnidFdvBgtVersion 
        {
            get
            {
                return _unidFdvBgtVersion;
            }
            set
            {
                if (value != _unidFdvBgtVersion)
                {
                    _unidFdvBgtVersion = value;
                }
            }
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (value != _name)
                {
                    _name = value;
                }
            }
        }

        public DateTime DatePublished
        {
            get
            {
                return _datePublished;
            }
            set
            {
                if (value != _datePublished)
                {
                    _datePublished = value;
                }
            }
        }

        public string PositionCode
        {
            get
            {
                return _positionCode;
            }
            set
            {
                if (value != _positionCode)
                {
                    _positionCode = value;
                }
            }
        }

        public DateTime DateLoaded
        {
            get
            {
                return _dateLoaded;
            }
            set
            {
                if (value != _dateLoaded)
                {
                    _dateLoaded = value;
                }
            }
        }

        public long RefUnidHqbgtVersion
        {
            get
            {
                return _refUnidHqbgtVersion;
            }
            set
            {
                if (value != _refUnidHqbgtVersion)
                {
                    _refUnidHqbgtVersion = value;
                }
            }
        }

        public DateTime HqDateClosed
        {
            get
            {
                return _hqDateClosed;
            }
            set
            {
                if (value != _hqDateClosed)
                {
                    _hqDateClosed = value;
                }
            }
        }
        #endregion

        public FdvVersion(long unidFdvBgtVersion, string name, DateTime datePublished
            , string positionCode, DateTime dateLoaded, long refUnidHqbgtVersion, DateTime hqDateClosed)
        {
            this._unidFdvBgtVersion = unidFdvBgtVersion;
            this._name = name;
            this._datePublished = datePublished;
            this._positionCode = positionCode;
            this._dateLoaded = dateLoaded;
            this._refUnidHqbgtVersion = refUnidHqbgtVersion;
            this._hqDateClosed = hqDateClosed;
        }
        public FdvVersion()
        {
        }

        public static FdvVersion GetFdvVersionByUnid(long unidFdvBgtVersion)
        {
            FdvVersion fv = null;
            SqlCommand sc = new SqlCommand();
            sc.CommandType = CommandType.StoredProcedure;
            sc.CommandText = "SP_GET_FDV_BGT_VERSION";
            sc.Parameters.Add(new SqlParameter("@unidFdvBgtVersion",unidFdvBgtVersion));

            try
            {
                DataSet res;
                res = SrvDB.ExecuteCommand(sc);

                if (res.Tables[0].Rows.Count > 0)
                {

                    fv = new FdvVersion(
                              res.Tables[0].Rows[0].IsNull("UNID_FDV_BGT_VERSION") ? 0 : (long)res.Tables[0].Rows[0]["UNID_FDV_BGT_VERSION"]
                              , res.Tables[0].Rows[0].IsNull("NAME") ? "" : (string)res.Tables[0].Rows[0]["NAME"]
                              , res.Tables[0].Rows[0].IsNull("DATE_PUBLISHED") ? DateTime.MinValue : (DateTime)res.Tables[0].Rows[0]["DATE_PUBLISHED"]
                              , res.Tables[0].Rows[0].IsNull("POSITION_CODE") ? "" : (string)res.Tables[0].Rows[0]["POSITION_CODE"]
                              , res.Tables[0].Rows[0].IsNull("DATE_LOADED") ? DateTime.MinValue : (DateTime)res.Tables[0].Rows[0]["DATE_LOADED"]
                              , res.Tables[0].Rows[0].IsNull("REF_UNID_HQBGT_VERSION") ? 0 : (long)res.Tables[0].Rows[0]["REF_UNID_HQBGT_VERSION"]
                              , res.Tables[0].Rows[0].IsNull("HQ_DATE_CLOSED") ? DateTime.MinValue : (DateTime)res.Tables[0].Rows[0]["HQ_DATE_CLOSED"]
                        );
                }
            }
            catch (Exception ex)
            {
                ;
            }

            return fv;
        }

        public static List<FdvVersion> GetListFdvVersionByTerritorio(string territorio)
        {
            List<FdvVersion> fdvList=null;
            SqlCommand sc = new SqlCommand();
            sc.CommandType = CommandType.StoredProcedure;
            sc.CommandText = "SP_GET_FDV_BGT_VERSION_BY_TERRITORIO";
            sc.Parameters.Add(new SqlParameter("@idTerritorio", territorio));

            try
            {
                DataSet res;
                res = SrvDB.ExecuteCommand(sc);

                fdvList=res.Tables[0].AsEnumerable().Select(
                    row =>
                        new FdvVersion(
                              row.IsNull("UNID_FDV_BGT_VERSION") ? 0 : (long)row["UNID_FDV_BGT_VERSION"]
                              , row.IsNull("NAME") ? "" : (string)row["NAME"]
                              , row.IsNull("DATE_PUBLISHED") ? DateTime.MinValue : (DateTime)row["DATE_PUBLISHED"]
                              , row.IsNull("POSITION_CODE") ? "" : (string)row["POSITION_CODE"]
                              , row.IsNull("DATE_LOADED") ? DateTime.MinValue : (DateTime)row["DATE_LOADED"]
                              , row.IsNull("REF_UNID_HQBGT_VERSION") ? 0 : (long)row["REF_UNID_HQBGT_VERSION"]
                              , row.IsNull("HQ_DATE_CLOSED") ? DateTime.MinValue : (DateTime)row["HQ_DATE_CLOSED"]
                        )
                ).ToList();
            }
            catch (Exception ex)
            {
                ;
            }

            return fdvList;
        }

        public static void UpdateFlagToPublish(long unidVersion, string idRecurso, bool flag)
        {
            SqlCommand cmd = new SqlCommand("SP_ER_SET_TOPUBLISHFLAG");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@UNID_VERSION", unidVersion));
            cmd.Parameters.Add(new SqlParameter("@ID_RECURSO", idRecurso));
            cmd.Parameters.Add(new SqlParameter("@VALUE", flag));


            try
            {
                SrvDB.ExecuteCommand(cmd);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


        }//

        public static void SetDatePublished(long unidVersion)
        {
            SqlCommand cmd = new SqlCommand("SP_SET_FDVBGT_VERSION_DATEPUBLISHED");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@unidVersion", unidVersion));

            try
            {
                SrvDB.ExecuteCommand(cmd);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }//



    }
}
